package com.example.repository;

import com.example.POJO.Blog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface BlogRepository extends JpaRepository<Blog, Long> {

  Page<Blog> findAll(Pageable pageable);


  /*
          CREATE TABLE `m_blog` (
            `id` bigint(20) NOT NULL AUTO_INCREMENT,
            `user_id` bigint(20) NOT NULL,
            `title` varchar(255) NOT NULL,
            `description` varchar(255) NOT NULL,
            `content` longtext,
            `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `status` tinyint(4) DEFAULT NULL,
            PRIMARY KEY (`id`)
          ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4;
   */

  @Modifying
  @Transactional
  @Query("update Blog b  set b.title=:title , b.description=:description, b.content=:content  where b.id = :id and b.userId=:userId")
  Integer update_myself(String title , String description , String content, Long id, Long userId);

  @Modifying
  @Transactional
  @Query(value = "   insert into m_blog(user_id,title,description,content)  values(:user_id, :title, :description, :content)  " , nativeQuery = true)
  Integer add_blog(long user_id , String title , String description, String content);

  Page<Blog> findByTitleContaining(String title, Pageable pageable);

  List<Blog> findByTitleContaining(String title, Sort sort);
}
